{% include 'QLGL/QLGL_index.html' %}

<div class="am-cf admin-main">
  {% include 'sidebar.html' %}
  <!-- content start -->
  <div class="admin-content">
    <div class="admin-content-body">
      <div class="am-cf am-padding">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">巡检业务</strong></div>
      </div>
      <div class="am-g" style="position: absolute;height: 100%;right: 0px;left: 260px;width: auto;">

        {% include 'QLGL/XJYW/QLGL_XJYW_SIDEBAR.html' %}
        
        <div class="am-u-sm-10" style="position: absolute;width:auto;right:0;left: 200px;overflow-y: scroll;bottom: 100px;top: 0px;">
          <div class="am-u-sm-12 am-u-md-6">
            <div class="am-btn-toolbar">
              <div class="am-btn-group am-btn-group-xs">
                <button style="margin: 0 5px"  type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="back_to()">返回</button>
                <button style="margin: 0 5px"  type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="add({{ taskInfo.0.check_id }})">保存</button>
              </div>
            </div>
          </div>


          <div class="am-form-group" style="margin-top: 60px">

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">桥梁名称：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='bridge_id' class="am-u-md-12"  data-am-selected="{searchBox: 1}" onchange="bridge_change()">

                  {% for i in bridgeInfo %}
                  {% if i.bridge_id == taskInfo.0.bridge_id %}
                  <option value={{ i.bridge_id }} selected="selected">{{ i.bridge_name }}</option>
                  {% else %}
                  <option value={{ i.bridge_id }} >{{ i.bridge_name }}</option>
                  {% endif %}
                   
                  {% endfor %}
                </select>

              </div>
              <div class="am-u-sm-5 am-u-md-2 am-text-right">责任单位：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='organize_id' class="am-u-md-10"  data-am-selected="{searchBox: 1}"  onchange="organize_change()">
                  {% for i in deptInfo %}


                  {% if i.organize_id == taskInfo.0.check_depart %}
                  <option value={{ i.organize_id }} selected="selected">{{ i.name }}</option> 
                  {% else %}
                  <option value={{ i.organize_id }} >{{ i.name }}</option> 
                  {% endif %}
                  
                  {% endfor %}
                </select>
              </div>          
            </div>

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">责任人：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='user_id' class="am-u-md-12"  data-am-selected="{searchBox: 1}" onchange="">
                {% for i in userInfo %}
                  {% if i.real_name == taskInfo.0.user_charge_name %}
                  <option value={{ i.user_id }} selected="selected">{{ i.real_name }}</option>
                  {% else %}
                  <option value={{ i.user_id }} >{{ i.real_name }}</option>
                  {% endif %}

                  {% endfor %}
                </select>
              </div>
              <div class="am-u-sm-5 am-u-md-2 am-text-right">开始时间：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <input type="date" class="input_width" id="from_date" value="{{ taskInfo.0.from_date }}"   />
              </div>          
            </div>

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">结束时间：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end"  >
                <input type="date" class="input_width" id="to_date" value="{{ taskInfo.0.to_date }}"  />

              </div>
              <div class="am-u-sm-5 am-u-md-3 am-text-right">
                <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="add_item()">新增检查项</button>
              </div>
              <div class="am-u-sm-7 am-u-md-2 am-u-end"  >
              </div>
            </div>
          </div>












          <table id="grid" class="am-table am-table-striped am-table-hover table-main am-table-compact am-text-nowrap" style="max-width: 1100px;padding-left: 8%;">
            <thead>
              <tr>
                <th class="table-title">序号</th>
                <th class="table-title">部件名称</th>
                <th class="table-title">检查项</th>
                <th class="table-title">操作</th>
              </tr>
            </thead>
            <tbody>

            </tbody>
          </table>

        </div>
      </div>
    </div>
  </div>
</div>
{% include 'footer.html' %}
<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="/static/assets/js/jquery.min.js"></script>
<!--<![endif]-->
<script src="/static/assets/js/amazeui.min.js"></script>
<script src="/static/assets/js/amazeui.tree.js"></script>
<script type="text/javascript" src="/static/assets/js/layer.js"></script>
<script src="/static/assets/datatables/amazeui.datatables.min.js"></script>
<script src="/static/assets/dialog/amazeui.dialog.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/assets/css/amazeui.tree.css" />
<script src="/static/assets/js/app.js"></script>
<style type="text/css">
  .li_a {
    color: #5c5c5c;
  }
  .input_width{
      width: 200px;
    }
</style>
<script type="text/javascript">
  $("#collapse-nav1").addClass("am-in");
  $("#qlgl").removeClass("am-collapsed");
  $("#qlglOxjyw").css("color", "#3399CC");
  $("#qlgl").css("color", "#3399CC");
  $("#xjywOjcjc").removeClass("li_a");
$.ajaxSetup({
  data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
});
  function back_to(){
    window.history.back(-1);
  }
  var checkitems =[];
  
  $(document).ready(function(){
    var checkitems = {{ checkitems|safe }};
    csh(checkitems);
  })
  function csh(checkitems){
    // var checkitems = JSON.parse(checkitems);
    for(var i=0;i<checkitems.length;i++){
      add_item_csh(checkitems[i]);
    }
  }

  function add_item_csh(checkitem){
  // $("#grid").css("display","table");
  // var code0 = $("#grid tbody tr:last").children('td').eq(0).text();
  // var code = parseInt(code0) + 1 ;
  $("#grid").children("tbody").append('<tr id="tr_'+code+'"><td>'+code+'</td><td><select id="tr_select1_'+code+'" class="input_width"  onchange=CHECKITEM_SUB_change("'+code+'")></select></td><td><select id="tr_select2_'+code+'"  class="input_width" ></select></td><td><button class="am-btn am-btn-default am-btn-xs am-text-secondary button_click" onclick="delect_one(\'tr_' + code + '\')">删除</button></td></tr>');
  CHECKITEM_change(''+code+'',checkitem.parts_sub_id,checkitem.bridge_parts_id);
  code+=1;
}

function organize_change(){
  var organize_id = $("#organize_id option:selected").val();

  $.ajax({
      "url":"/QLXJ_XJYW_DQJC_USER/?organize_id="+organize_id+"",
      "datatype":"json",
      "type":"get",
      "async":"false"
      }).success(function(data){
        if (data.status == 1) {
          $("#user_id").empty();
          for(var i=0;i<data.data.length;i++){
            $('#user_id').append('<option value='+data.data[i].user_id+' >'+data.data[i].real_name+'</option>');
          }

    

        } else {
          AlertMSG(data.message)
        }
      }).error(function(data){
        AlertMSG("读取负责人失败！")
      });
}


function bridge_change(){
  $("#grid").children("tbody").empty();
}
var code = 1;
function add_item(){
  // $("#grid").css("display","table");
  // var code0 = $("#grid tbody tr:last").children('td').eq(0).text();
  // var code = parseInt(code0) + 1 ;
  $("#grid").children("tbody").append('<tr id="tr_'+code+'"><td>'+code+'</td><td><select id="tr_select1_'+code+'" class="input_width"  onchange=CHECKITEM_SUB_change("'+code+'")></select></td><td><select id="tr_select2_'+code+'"  class="input_width" ></select></td><td><button class="am-btn am-btn-default am-btn-xs am-text-secondary button_click" onclick="delect_one(\'tr_' + code + '\')">删除</button></td></tr>');
  CHECKITEM_change(''+code+'',0,0);
  code+=1;
}



  function delect_one(id) {
    $("#"+id).remove();
  }

  var AlertMSG=function(msg){
    AMUI.dialog.alert({ title: '错误提示', content: msg, onConfirm: function() { console.log('close'); } });
  }


function CHECKITEM_change(code,parts_sub_id,bridge_parts_id){
  var bridge_id = $("#bridge_id option:selected").val();

  $.ajax({
      "url":"/QLXJ_XJYW_JCJC_ADD_CHECKITEM/?bridge_id="+bridge_id,
      "datatype":"json",
      "type":"get",
      "async":"false"
      }).success(function(data){
        if (data.status == 1) {
          $("#tr_select1_"+code).empty();
          for(var i=0;i<data.data.partsList.length;i++){
            if(data.data.partsList[i].partsid == bridge_parts_id){
              $('#tr_select1_'+code).append('<option value='+data.data.partsList[i].partsid+' selected="selected">'+data.data.partsList[i].partsname+'</option>');
            }else{
              $('#tr_select1_'+code).append('<option value='+data.data.partsList[i].partsid+' >'+data.data.partsList[i].partsname+'</option>');
            }
            
          }
          CHECKITEM_SUB_change(code,parts_sub_id);
    

        } else {
          AlertMSG(data.message)
        }
      }).error(function(data){
        AlertMSG("读取信息失败！")
      });
}
function CHECKITEM_SUB_change(code,parts_sub_id){
  var parts_id = $("#tr_select1_"+code).val();

  $.ajax({
      "url":"/QLXJ_XJYW_JCJC_ADD_CHECKITEM_SUB/?parts_id="+parts_id,
      "datatype":"json",
      "type":"get",
      "async":"false"
      }).success(function(data){
        if (data.status == 1) {
          $("#tr_select2_"+code).empty();
          console.log(parts_sub_id)
          for(var i=0;i<data.data.length;i++){
            console.log(data.data[i].parts_sub_id)
            if (data.data[i].parts_sub_id == parts_sub_id) {

              $('#tr_select2_'+code).append('<option value='+data.data[i].parts_sub_id+' selected="selected">'+data.data[i].name+'</option>');
            }else{
              $('#tr_select2_'+code).append('<option value='+data.data[i].parts_sub_id+' >'+data.data[i].name+'</option>');
            }
            
          }

    

        } else {
          AlertMSG(data.message)
        }
      }).error(function(data){
        AlertMSG("读取信息失败！")
      });
}












function add(check_id){
  var bridge_id = $("#bridge_id option:selected").val();
  var bridge_name = $("#bridge_id option:selected").text();
  var user_id = $("#user_id option:selected").val();
  var real_name = $("#user_id option:selected").text();
  var organize_id = $("#organize_id option:selected").val();
  var organize_name = $("#organize_id option:selected").text();
  var from_date = $("#from_date").val();
  var to_date = $("#to_date").val();
  var checkitems = '';
  var itemdata = [];
  $("#grid tbody tr").each(function(i){
    var notes = $(this).children('td').eq(2).children('select');
    var id = notes.find('option:selected').val();
    
    if(itemdata.indexOf(id) == -1){
      if(checkitems == ''){
        checkitems+=id.toString();
      }else{
        checkitems+= '|' + id.toString();
      }
    }
    
    itemdata.push(id);
  })
  // console.log(checkitems);


  if(bridge_id==undefined){
    AlertMSG("请选择桥梁！")
  }else if (organize_id==undefined){
    AlertMSG("请选择责任单位！")
  }else if (user_id==undefined){
    AlertMSG("请选择责任人！")
  }else if (from_date==''){
    AlertMSG("请选择开始时间！")
  }else if (to_date==''){
    AlertMSG("请选择结束时间！")
  }else if (to_date <= from_date){
    AlertMSG("开始时间应小于结束时间！")
  }else if (checkitems == ''){
    AlertMSG("请添加检查项！")
  }else {
    $.ajax({
      "url":"/QLXJ_XJYW_JCJC_MODIFYSAVE/",
      "data":{"bridge_id":bridge_id,"bridge_name":bridge_name,"user_id":user_id,"real_name":real_name,"organize_id":organize_id,"organize_name":organize_name,"from_date":from_date,"to_date":to_date,"checkitems":checkitems,"check_id":check_id},
      "datatype":"json",
      "type":"post",
      "async":"false"
    }).success(function(data){
      if (data.status == 1) {
        // message_layer(data.message)
        back_to();

      } else {
        AlertMSG(data.message)
      }
    }).error(function(data){
      AlertMSG("保存失败！")
    });
  }


}
// function findWeather() {
//   var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
//   $.getScript(cityUrl, function(script, textStatus, jqXHR) {
//     var citytq = remote_ip_info.city ;// 获取城市
//     var url = "http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&city=" + citytq + "&day=0&dfc=3";
//     $.ajax({
//       url : url,
//       dataType : "script",
//       scriptCharset : "gbk",
//       success : function(data) {
//         var _w = window.SWther.w[citytq][0];
//         var _f= _w.f1+"_0.png";
//         if(new Date().getHours() > 17){
//            _f= _w.f2+"_1.png";        
//         }
//         var img = "<img width='16px' height='16px' src='http://i2.sinaimg.cn/dy/main/weather/weatherplugin/wthIco/20_20/" +_f
//         + "' />";
//         var tq = citytq + " " + img + " " + _w.s1 + " " + _w.t1 + "℃～" + _w.t2 + "℃ " + _w.d1 + _w.p1 + "级";
         
//         console.log(tq)
//       }
//     });
//   });
// }
 
// findWeather();





</script>
 
</body>

</html>